package logic

import (
	"context"
	"good_rpc/internal/svc"
	"good_rpc/pb/good"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodByIDListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodByIDListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodByIDListLogic {
	return &GetGoodByIDListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodByIDListLogic) GetGoodByIDList(in *good.IDListRequest) (*good.IDListResponse, error) {
	goods, err := l.svcCtx.GoodModel.FindListByIDList(l.ctx, in.Ids)
	if err != nil {
		l.Logger.Errorf("GetGoodByIDList failed: ids=%v, error: %v", in.Ids, err)
		return nil, err
	}
	var data = make([]*good.GoodInfo, len(goods))
	for index, v := range goods {
		data[index] = &good.GoodInfo{Id: v.Id, Name: v.Name, Price: v.Price, Stock: v.Stock}
	}
	return &good.IDListResponse{Data: data}, nil
}
